home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 16
/
Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso
/
Aminet
/
util
/
misc
/
cookietool.lha
/
cookietool
/
cdbsplit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-10-03
|
6KB
|
222 lines
; /*
gcc cdbsplit.c -O -noixemul -o cdbsplit
quit 0 ; */
/*************************************************************************\
cdbsplit: split your cookie database in two,
by keyword, by line length or by number of lines.
Expected file format is plain text with a "%%" line ending each cookie.
Usage:
cdbsplit [options] infile [outfile1] [outfile2]
options: meaning:
-k<keywd> search for a keyword
-K<keywd> search for a keyword, case sensitive
-l<l_max> only cookies with up to <l_max> lines
-w<w_max> only cookies up to <w_max> chars wide
Output file names default to <infile>.hit and <infile>.dump.
If outfile1 but no outfile2 is specified, the dumpfile will overwrite
the input file.
\*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char version[] = "$VER: cdbsplit 1.3 (03.10.96)";
#define CBUFSIZE 20000
#define LBUFSIZE 2000
char line[LBUFSIZE]; /* large enough to hold the longest line */
char cbuf[CBUFSIZE]; /* large enough to hold one complete cookie */
char cbak[CBUFSIZE]; /* backup after uppercase conversion */
char uppercase[256]; /* conversion table */
int case_sense=0, l_min=0, l_max=0, w_min=0, w_max=0;
char target[100];
void help(char *s)
/* print a help text and nag about illegal parameter <s> */
{
if (s) printf("illegal option '%s'\n", s);
printf("usage: cdbsplit <options> <cookiefile> [hitfile] [dumpfile] \n");
printf("where options are:\n");
printf(" -k<keyword> search for a keyword\n");
printf(" -K<keyword> \" , case sensitive\n");
printf(" -l<lines> / -L<lines> range for number of lines in a cookie\n");
printf(" -w<width> / -W<width> range for cookie line width\n");
}
void init_tab()
/* set up a conversion table that can convert all national characters from
* the ISO 8859-1/ECMA 94 charset to uppercase, not only 'a'-'z'.
*/
{
int c;
for (c=0; c<256; c++) uppercase[c] = c;
for (c='a'; c<='z'; c++) uppercase[c] = toupper(c);
/* if (!ignore_iso) */
for (c=224; c<255; c++) /* don't convert #255 ("y) !!! */
if (c != 247) /* 247 is the division sign -:- */
uppercase[c] = c-32;
}
void makeupper(char *s)
{
while(*s) {
*s = uppercase[(unsigned char) *s];
s++;
}
}
void filter_cookies(FILE *fp1, FILE *fp2, FILE *fp3)
{
long count=0, hits=0, cbuflen;
int ok, lines, width, w;
char *printme;
strcpy(cbuf,""); cbuflen = lines = width = 0;
while (fgets(line,LBUFSIZE,fp1)) {
if (strncmp(line,"%%",2)==0) { /* "end of cookie"-marker */
if (!case_sense) {
strcpy(cbak, cbuf);
makeupper(cbuf); printme = cbak;
} else
printme = cbuf;
/* perform the checks: */
ok = (lines >= l_min) && (width >= w_min);
if (l_max) ok = ok && (lines <= l_max);
if (w_max) ok = ok && (width <= w_max);
if (target[0]) ok = ok && strstr(cbuf, target);
if (ok) { /* "good" cookie, copy it */
hits++;
fputs(printme, fp2);
fputs("%%\n", fp2);
} else if (fp3) { /* copy others only if requested */
fputs(printme, fp3);
fputs("%%\n", fp3);
}
count++;
if (count%100 == 0) {
printf("Copying cookies, %ld hits, %ld misses.\r", hits, count-hits);
fflush(stdout);
}
strcpy(cbuf,""); /* start a new cookie */
cbuflen = lines = width = 0;
} else {
w = strlen(line);
if ((cbuflen += w) >= CBUFSIZE) {
printf("\ncookie too big (>%ld chars)\n", CBUFSIZE);
exit(20);
}
strcat(cbuf,line); lines++;
if (w > width) width = w;
}
}
printf("\nDone, %ld hits out of %ld.\n", hits, count);
}
int main(int argc, char *argv[])
{
char *s;
char name1[100], name2[100], name3[100];
int writeback = 0;
FILE *infile, *hitfile, *dumpfile;
name1[0] = name2[0] = name3[0] = target[0] = '\0';
if (argc < 3) {
help(NULL);
return 5;
}
while (--argc) {
s = *++argv;
if (*s != '-') {
if (name1[0] == '\0')
strcpy(name1, s);
else if (name2[0] == '\0')
strcpy(name2, s);
else
strcpy(name3, s);
} else {
switch (*++s) {
case 'k':
strcpy(target, ++s);
case_sense = 0; break;
case 'K':
strcpy(target, ++s);
case_sense = 1; break;
case 'l':
l_min = atoi(++s); break;
case 'L':
l_max = atoi(++s); break;
case 'w':
w_min = atoi(++s); break;
case 'W':
w_max = atoi(++s); break;
default:
help(argv[0]); return 5;
}
}
}
if (name1[0] == '\0') {
help(NULL);
return 5;
}
if (name2[0] == '\0') {
strcpy(name2,name1); strcat(name2,".hit");
strcpy(name3,name1); strcat(name3,".dump");
}
if (name3[0] == '\0') {
strcpy(name3,"cdb_temp_kickme");
writeback = 1;
}
if (!(infile = fopen(name1,"r"))) {
printf("Can't open '%s' for input!\n", name1);
return 10;
}
if (writeback && (hitfile = fopen(name2,"r"))) {
printf("Error: shouldn't overwrite '%s'!\n", name2);
return 10;
}
if (!(hitfile = fopen(name2,"w"))) {
printf("Can't open '%s' for output!\n", name2);
return 10;
}
if (!(dumpfile = fopen(name3,"w"))) {
printf("Can't open '%s' for output!\n", name3);
return 10;
}
init_tab();
if (!case_sense)
makeupper(target);
if (writeback)
printf("Extracting from '%s' to '%s',\n", name1, name2);
else
printf("Splitting '%s' into '%s' and '%s',\n", name1, name2, name3);
if (target[0] != '\0')
printf(" search string is \"%s\".\n", target);
if (l_max)
printf(" looking for cookies %d - %d lines long.\n", l_min, l_max);
else if (l_min)
printf(" looking for cookies at least %d lines long.\n", l_min);
if (w_max)
printf(" looking for cookies %d - %d columns wide.\n", w_min, w_max);
else if (w_min)
printf(" looking for cookies at least %d columns wide.\n", w_min);
/* OK, here we go: */
filter_cookies(infile,hitfile,dumpfile);
fclose(infile); fclose(hitfile); fclose(dumpfile);
if (writeback) {
if (remove(name1) !=0 || rename(name3, name1) != 0) {
printf("Warning: couldn't overwrite the input file!\n");
return 5;
}
}
return 0;
}